//============================================================================
// Name        : BestTry.cpp
// Author      : Marc Petroff
// Version     :
// Copyright   : MPRights (2014)
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <cstdlib>
#include <iostream>
#include <cstdarg>
//#include <stdlib>
//#include <dlfcn>

#undef MP_CRYPTLIB_INTERNAL_DECLARATION
#include "mp_crypt.h"

#undef VAL_ARGS_ENABLED


enum mp_crypt_error {
	MP_CRYPTERR_NO_ERR = 1,                    //!< MP_CRYPTERR_NO_ERR
	MP_CRYPTERR_EMPTY_PUBLIC_STRING = -8892,   //!< MP_CRYPTERR_EMPTY_PUBLIC_STRING
	MP_CRYPTERR_TOO_LONG_PUBLIC_STRING = -8893,//!< MP_CRYPTERR_TOO_LONG_PUBLIC_STRING
	MP_CRYPTERR_LIBRARY_DEPENDANCY_FAIL = -8894//!< MP_CRYPTERR_LIBRARY_DEPENDANCY_FAIL
} e;


struct peoi_html_placeholer_struct {
	char *PEOI_HTML_PLACEHOLDER_FORMNAME; //!< %FORMNAME%
	char *PEOI_HTML_PLACEHOLDER_DIVNAME; //!< "%DIVNAME%"
} html_placeholder {"%FORMNAME%","%DIVNAME%"};

struct trackchain_struct {
	char *trackMessage;
	trackchain_struct *child;
} ;


/**
 * La variable globale qui contient le code erreur basé sur l'enum
 */
mp_crypt_error mp_errno;

int peoi_sprintf(char *strbuffer,const char *stemplate,unsigned int max,...) {
	int sprintf_ret = 0;
char *kkze[7];

//va_list copy;

	if (strlen(strbuffer) +1 < max) {

		va_list args;
		//va_copy(copy,args);



// TODO remove tracking instruction
		//printf("Using PRINTF %s/\n %s/\n %s/\n %s/\n %s/\n %s/\n %s/\n",args);
		va_start(args,max);
		for (int i = 0; i < 7; ++i) {
			kkze[i] = (char *)malloc(256);
			strcpy(kkze[i],(const char*)va_arg(args,char **));
			printf("%s \n",kkze[i]);
		}
		std::cout << " in peoi_sprintf "<< strbuffer << " with " << stemplate << " for max " << max << " with args " << kkze[0] << kkze[4] << " end args" << std::endl;
		//            sprintf            commande standard
		//sprintf_ret = sprintf(strbuffer,stemplate,args);
		//va_end(args);
	}
	else {   //  if (strlen(
		//   ECHEC  racourcir la chaine et préparer valeur retour 0
		strbuffer[0] = '\0';
		sprintf_ret = 0;
	} //  if (strlen(
	return sprintf_ret;
}

int peoi_track_add_log(trackchain_struct *chaintrack,char *loglinestr, unsigned int maxloglinesize) {
	int z = 0;

	 trackchain_struct *tracklink;
	tracklink = (trackchain_struct *)malloc(sizeof(trackchain_struct));
	tracklink->trackMessage = (char *)malloc(maxloglinesize + 1);
	tracklink->child = (trackchain_struct *)malloc(sizeof(trackchain_struct));

	strcpy (tracklink->trackMessage,loglinestr);
	tracklink->child = 0;

	// link the new set of strings to the previous end of the chain
	chaintrack->child = tracklink;

	chaintrack = tracklink;

	return 1;
}

int peoi_readline(char *line, char *name,unsigned int u,FILE *f,char **trace)
{int cc,h,i,j,k,v,vv,z,maxtrackline;
unsigned int x;
char *value[1];
trackchain_struct *curtrack;
curtrack = (trackchain_struct *)malloc(sizeof(trackchain_struct));

char *track[0];
cc=h=i=j=k=v=vv=0;

z = 0;
maxtrackline = 256;


track[z] = (char *)malloc(maxtrackline + 1);

trackchain_struct headofchaintrack;

headofchaintrack.trackMessage = (char *)malloc(maxtrackline + 1);
headofchaintrack.child = (trackchain_struct *)malloc(sizeof(trackchain_struct));

trackchain_struct chaintrack;

chaintrack.trackMessage = (char *)malloc(maxtrackline + 1);
chaintrack.child = (trackchain_struct *)malloc(sizeof(trackchain_struct));

strcpy(headofchaintrack.trackMessage,"THIS IS THE HEAD");

strcpy(chaintrack.trackMessage,"THIS IS THE FIRST CHILD");

headofchaintrack.child = &chaintrack;


std::cout << headofchaintrack.trackMessage << " Is the head " << std::endl;

std::cout << headofchaintrack.child->trackMessage << " Is the chiled" << std::endl;


value[z] = (char *)malloc(u + 1);

strcpy(value[z]," *******  MASSIVE FAILURE ****");
track[z][0] = '\0';

#ifdef VAL_ARGS_ENABLED
if(u < 257) peoi_sprintf(track[z],"<BR>start readline j=%s u=%s vv=%s k=%s v=%s value=%s/ name=%s/\n",maxtrackline,
"jji","u","vv","k","v","oh","og");
#else
if(u < 257) sprintf(track[z],"<BR>start readline j=%s u=%s vv=%s k=%s v=%s value=%s/ name=%s/\n",
"jji","u","vv","k","v","oh","og");
#endif

std::cout << " track returned with " << track[z] << std::endl;

curtrack = &chaintrack;

strcpy(curtrack->trackMessage," ***  HOLY COW ***");

peoi_track_add_log( &chaintrack,track[z],maxtrackline);

std::cout << curtrack->trackMessage << " ** COPIED REF ** Is return from add log" << std::endl;
std::cout << chaintrack.trackMessage << " ***  OBJECT ** Is return from add log" << std::endl;

     if(feof(f)) {
    	  //if the file is at the end of file then we set the output string to null string
#ifdef VAL_ARGS_ENABLED
    	 peoi_sprintf(track[z],"<BR>reached end of file; name=%s/\n",maxtrackline,name);
#else
    	 sprintf(track[z],"<BR>reached end of file; name=%s/\n",name);

#endif
    	 peoi_track_add_log(curtrack,track[z],maxtrackline);

    	 std::cout << curtrack->trackMessage << " Is return from SECOND add log" << std::endl;
   	 value[z][0] = '\0';
     }
      else if(!fgets(value[z],u,f)) {
    	  j=k=v=vv=0;
    	   	// peoi_sprintf(track[z],"<BR>Empty line because at end of file; name=%i/\n",maxtrackline,name);
    	    	 value[z][0] = '\0';
    	      }
      else {

           if((j=strlen(value[z]))>u){
 	             // peoi_sprintf(track[z],"<BR>line too long j=%i u=%i value=%i <BR>=\n",maxtrackline,j,u,value[z]);
                      /*k=0;while(k<100){peoi_sprintf(track[z],"%d/",maxtrackline,value[z][k]);k++;}*/
                    //  peoi_sprintf(track[z],"<BR>",maxtrackline,u);
           }
#ifdef VAL_ARGS_ENABLED
    	 peoi_sprintf(track[z],"<BR> Still going strong; name=%s/\n",maxtrackline,name);
#else
    	 sprintf(track[z],"<BR>Still going strong; name=%s/\n",name);

#endif
    	 peoi_track_add_log(curtrack,track[z],maxtrackline);

      	 std::cout << curtrack->trackMessage << " Is return from THIRD add log" << std::endl;
      }

     strcpy(line,value[z]);

     //trace = headofchaintrack;
     /*free(value);
     free(track);
     free(headofchaintrack);
     free(chaintrack);*/

      return(j);
}

void printscriptchangecol(char *formname,char *insertword)
{int cc;cc=0;
        printf("<SCRIPT LANGUAGE=\"JavaScript\">\n");
	printf(" <!--\n ");

printf("var iin=0; \n");
printf("var iout=0; \n");
printf("var ee='document.%s.anchortextend'; \n",formname);
printf("var e='document.%s.anchortextend'; \n",formname);
printf("var name=''; \n");
printf("var pastanchor='anchortextend'; \n");
printf("var pastanchorvalue='textend'; \n");
printf("var currentanchor='anchortextend'; \n");

printf("function chgcol2(anchname,numcol){ \n");
      printf(" var f = document.%s; \n",formname);
printf("currentanchor=anchname; \n");
printf("iin=numcol; \n");
printf("ancnum=numcol; \n");
if(cc)printf("if(geno>1)geno=0; \n");
printf("e = eval(\"document.%s.\" + anchname); \n",formname);
printf("ee = eval(\"document.%s.\" + pastanchor); \n",formname);
if(cc)printf("e.value='%s '+anchname; \n",insertword);

printf("e.value=dowhat+' '+anchname; \n");
printf("if(currentanchor!=pastanchor){ \n");
printf("e.className='btnclick'; \n");
printf("ee.className='btnhov'; \n");
printf("ee.value=pastanchor; \n");
printf("} \n");
if(!cc)printf("var name=' numcol='+numcol+' dowhat= '+dowhat+'/ currentanchor='+anchname+' pastanchor='+pastanchor+' value='+e.value+' type='+e.type+' class='+e.className+' pastanchor='+pastanchor+' numcol='+numcol;");
if(cc)printf("ee=e; \n");
printf("pastanchor=anchname; \n");
printf("document.%s.sanchor.value=numcol; \n",formname);
printf("if(iin>999 && confirm('%s '+anchname+' saction='+saction+'  ancnum='+ancnum+' numcol='+iin+' all')){iin=numcol; } \n",formname);

printf("if(iin>999 && confirm(name));\n");
printf("} \n");


printf("function setcol(anchname,numcol){ \n");
printf("currentanchor=anchname; \n");
printf("iin=numcol; \n");
printf("var msg = 'formname=%s numcol='+numcol+' anchname='+anchname;",formname);
printf("if(iin>999 && confirm(msg)){iin=numcol; } \n");
printf("var e = eval(\"document.%s.\" + anchname); \n",formname);

printf("e.value=dowhat+' '+anchname; \n");
printf("var dumvar=''; \n");
if(cc)printf("if(geno==1)geno='0';");
printf("e.className='btnclick'; \n");
printf("document.%s.sanchor.value=numcol; \n",formname);
printf("}\n");

printf("function removecol(pastanchor){ \n");
printf("var eee = eval(\"document.%s.\" + pastanchor); \n",formname);
printf("eee.value=dowhat+' '+pastanchor; \n");
printf("eee.className='btnclick'; \n");
printf("if(iin>999 && confirm(name)); \n");
printf("}");


	printf(" // --></SCRIPT>\n");

}

void resizescript()
{int cc;
cc=0;
printf("  <SCRIPT LANGUAGE=\"JavaScript\">\n");
printf(" <!-- \n");
printf(" var i = 1; \n");
printf(" var u = 1; \n");
printf(" var g=0;\n");
printf(" var oldsize=3;\n");
printf(" var newsize=3;\n");
printf(" if(i>99)document.write('<BR> first cookie'+document.cookie);\n");
printf(" if((u=GetCookie('initSize')) && u>-1 && u<9)newsize=u;\n");
printf(" g=newsize-oldsize;\n");
printf("       if(g)fontSizer(g,'px');\n");

printf(" if(i>99)document.write('<BR>oldsize='+oldsize+' newsize='+newsize+' g='+g+' initSize='+initSize+'  pixelArray['+initSize+']='+pixelArray[initSize]);\n");

printf(" //  End -->\n");
printf("   </SCRIPT>\n");
}
int printverifyscript2()
{int cc,i;
cc=i=0;
      printf("<SCRIPT LANGUAGE=\"JavaScript\"> \n");
	printf(" function verifyrating() { \n");
	printf(" var msg2 = 'Please, explain rating in comment!'; \n");
      printf(" var kk=0;var good=1; \n");
      printf(" var f = document.f2; \n");
	printf(" for (var i = 0; i<2; i++) { \n");
      printf(" var box = eval(\"f.newrate\"); \n");
	printf(" if(box[i].checked == 1 && f.comments.value.length<2)kk=1;}\n");
	printf(" if(kk>0){alert(msg2); return false; \n");
	printf(" } \n");
	printf(" return true;} \n");
	printf(" // --></SCRIPT>\n");
  return(i);
}
using namespace std;

int main() {

	void *l[4];
	char **d;
	int u;
	void *library_handle;
	char *peoi_trace[1];

	peoi_trace[0] = (char *)malloc(1024);

	printscriptchangecol("%FORMNAME%","%INSERTWORD%");
	resizescript();
	u = printverifyscript2();
/*
library_handle = dlopen("libMP_cryptlib.so.0.08.0005",RTLD_DI_LMID);

		// TODO implement the library !

if (library_handle == NULL) {
 dlsym(library_handle,"mp_getEncrypt");
}
*/

struct B {
	char **j;
	int z;
} b,e;


B *s;
d = (char **)calloc(2,sizeof(char));
d[0] = (char *)calloc(255,sizeof(char));
d[1] = (char *)calloc(255,sizeof(char));

/*if ((u = mp_getVersion(d[0]))) {
	std::cout << (char *)d[0] << std::endl;
}*/

//l = calloc(2,sizeof(char));
l[0] = calloc(255,sizeof(char));
l[1] = calloc(255,sizeof(char));
l[2] = calloc(255,sizeof(char));
l[3] = NULL;
strcpy((char *)l[0],"Somestuff");
strcpy((char *)l[1],"Unwantedcrap");

// **********************************************     PEOI  STUFF
char buffr[8000];
const char *nn = "j.html";
unsigned int maxlineread = 256;
FILE *filehtml;
filehtml = fopen((char *)nn,"r");
if (filehtml) {
//fread((void *)buffr,sizeof(char),8000,filehtml);
	buffr[maxlineread + 1] = '\0';
	if (peoi_readline(buffr,(char *)nn,maxlineread,filehtml,peoi_trace)) {
         // bad stuff
	}
}
else {
	strcpy(buffr,"FAILED ******************************************* \n");
}


printf(" START FILE CONTENTS %s  END FILE CONTENTS", buffr);
if (filehtml) {
//fread((void *)buffr,sizeof(char),8000,filehtml);
	buffr[maxlineread + 1] = '\0';
	if (peoi_readline(buffr,(char *)nn,maxlineread,filehtml,peoi_trace)) {
         // bad stuff
	}
}
else {
	strcpy(buffr,"FAILED ******************************************* \n");
}


printf(" ANOTHER START FILE CONTENTS %s  END FILE CONTENTS", buffr);
// **********************************************     PEOI  STUFF   END ****

b.j = (char **)l;
e.j = (char **)l;
//setenv("LD_LIBRARY_PATH","/home/marc/workspace/BestTry/Release:$LD_LIBRARY_PATH",)

e.z = mp_getVersion(d);
free(d);
d = (char **)calloc(2,sizeof(char));
d[0] = (char *)calloc(255,sizeof(char));
d[1] = (char *)calloc(255,sizeof(char));

strcat(d[0],"Long words reversed");

e.z = mp_getEncrypt(d[0]);

strcpy(e.j[1],"Morecrap");
s->j = (char **)l;
strcpy((char *)l[2],"After insert");

	cout << "[GB_en]Hello World " << d[0] << " returned int : " << e.z << "detailed version :" <<  d[1] << b.j[0] << " " << e.j[1] << " " << s->j[2] << endl; // prints [GB_en]Hello World
    cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl;
    cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
    cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
    cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
    cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
    cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
    cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
    cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
   // using namespace mp_crypt_h;
    cout << "sctruct:\t\t" << mp_crypt_h::mp_crypt_types.MP_CRYPTTYPE_REVERSE   << " value" << endl;
    cout << "summ:\t\t" << mp_setEncryptType((int)mp_crypt_h::mp_crypt_types.MP_CRYPTTYPE_REVERSE)  << " value" << endl;
    cout << "summ should faill (9999):\t\t" << mp_setEncryptType(31)  << " value" << endl;
   cout << "long double:\t" << sizeof(long double) << " bytes" << endl;
// mp_setEncryptType
	/*Free the memory tha was calloc'd'*/
	free(d);
	free(l[0]);free(l[1]);free(l[2]);free(l[3]);
	return 0;
}
